home *** CD-ROM | disk | FTP | other *** search
- /* misc.c -- misc routines for Javelin UNIX daemon.
- *
- * %W%
- *
- * Author: Scott Mulligan
- * Copyright 1993 Apple Computer, Inc.
- * All Rights Reserved.
- *
- * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF APPLE COMPUTER, INC.
- * The copyright notice above does not evidence any actual or
- * intended publication of such source code.
- *
- ************************************************************************
- * functions/routines contained herein:
- *
- * mymalloc()
- * shutdown_now()
- * chk_timeout()
- * chklock()
- * onelock()
- * set_lock()
- * tell_mac()
- * chkaccess()
- * send_status()
- */
-
- #include <stdio.h>
- #include <pwd.h>
- #include <sys/errno.h>
- #include <sys/id.h>
- #include <sys/select.h>
- #include <sys/types.h>
-
- #include "Types.h"
- #include "AppleEvents.h"
- #include "AUXAESuite.h"
- #include "AppleEvents.h"
-
- #include <sys/file.h> /* For some reason, if I put this earlier I get an error in "Memory.h" */
-
- #include "mandel.h"
- #include "version.h"
-
- char tmpcmddata[512];
- char cmddatabuf[5120];
-
- extern boolean TimeToQuit;
- extern AEAddressDesc target;
- extern int debuglevel;
- extern int sessionid;
- extern int missed_heartbeats;
- extern char *databuf;
- extern char OSVersion[32];
- extern char *clientVersion;
- extern char *serverVersion;
- extern int errno;
- extern time_t time_of_last_update; /* last time we needed to send an update */
- extern time_t time_of_last_recv; /* last heartbeat received */
- extern time_t time_of_last_check; /* last time we checked how long since we recv'd */
- extern time_t time_of_last_send; /* last time we sent a heartbeat */
- extern FILE *debugfp;
-
-
-
- void *mymalloc(size)
- size_t size;
- {
- int *ptr;
-
- ptr = malloc(size);
- if (ptr == NULL) {
- #ifdef DEBUG
- fprintf(debugfp,"\n\n* * * malloc() FAILED. errno = (%d) * * *\n\n",errno);
- if (errno == EINVAL)
- fprintf(debugfp,"\n\n* * * errno = (EINVAL) * * *\n\n");
- if (errno == ENOMEM)
- fprintf(debugfp,"\n\n* * * errno = (ENOMEM) * * *\n\n");
- fflush(debugfp);
- sleep(1);
- #endif
- return((void *)NULL);
- }
- else {
- return((void *)ptr);
- }
- }
-
-
-
- void shutdown_now()
- {
- #ifdef DEBUG
- fprintf(debugfp,"SHUTDOWN.\n\n");
- fflush(debugfp);
- #endif
- if (unlink(LOCKFILE) != 0) {
- #ifdef DEBUG
- fprintf(debugfp,"unlink() failed in shutdown_now().\n\n");
- fflush(debugfp);
- #endif
- }
- #ifdef DEBUG
- fprintf(debugfp,"3..."); fflush(debugfp);
- sleep(1);
- fprintf(debugfp,"2..."); fflush(debugfp);
- sleep(1);
- fprintf(debugfp,"1..."); fflush(debugfp);
- sleep(1);
- fprintf(debugfp,"\n"); fflush(debugfp);
- #else
- sleep(3);
- #endif
- exit (0);
- }
-
-
- void chk_heartbeat()
- {
-
- #ifdef DEBUG
- fprintf(debugfp,"In chk_heartbeat().\n");
- fflush(debugfp);
- #endif
-
- time(&time_of_last_check);
- if ((time_of_last_check - time_of_last_recv) > HEARTBEAT_CHECK) {
- #ifdef DEBUG
- fprintf(debugfp,"chktimeout(): Missed a heartbeat.........\n");
- fflush(debugfp);
- #endif
- missed_heartbeats++;
- /* time_of_last_recv = time_of_last_check; /* */
- if (missed_heartbeats >= MAX_MISSED_HEARTBEATS) {
- #ifdef DEBUG
- fprintf(debugfp,"chktimeout(): MAX_MISSED_HEARTBEATS ");
- fprintf(debugfp,"exceeded. Calling shutdown_now().\n");
- fflush(debugfp);
- #endif
- TimeToQuit=TRUE; /* */
- }
- }
- else
- missed_heartbeats = 0;
-
- #ifdef DEBUG
- fprintf(debugfp,"Leaving chk_heartbeat().\n");
- fflush(debugfp);
- #endif
- return;
- }
-
-
-
- void snd_heartbeat()
- {
- AppleEvent theAppleEvent;
- AppleEvent reply;
- AESendMode sendMode;
- int sessionID=1;
- AESendPriority sendPriority;
- AEDesc target;
- long timeOutInTicks;
- int retval;
-
- #ifdef DEBUG
- fprintf(debugfp,"In send_heartbeat().\n");
- fflush(debugfp);
- #endif
-
-
- (void)time(&time_of_last_send);
-
- (void)AECreateDesc(typeSessionID,&sessionID,sizeof(sessionID),&target);
-
- (void)AECreateAppleEvent(kAEAUXSuite,kAEHeartBeat,&target,
- kAutoGenerateReturnID,kAnyTransactionID,&theAppleEvent);
-
- (void)AEPutParamPtr(&theAppleEvent,keySessionID,typeInteger,&sessionid,sizeof(sessionid));
-
- sendMode = kAENoReply;
- sendPriority = kAENormalPriority;
- timeOutInTicks = kAEDefaultTimeout;
-
- retval=AESend(&theAppleEvent,&reply,sendMode,sendPriority,timeOutInTicks,NULL,NULL);
- #ifdef DEBUG
- fprintf(debugfp,"AESend's retval = (%d).\n",retval);
- fflush(debugfp);
- #endif
-
- AEDisposeDesc(&target);
- AEDisposeDesc(&theAppleEvent);
-
- #ifdef DEBUG
- fprintf(debugfp," thummmmp... thummmmp...\n");
- fprintf(debugfp,"Leaving send_heartbeat().\n");
- fflush(debugfp);
- #endif
-
- }
-
-
-
- int chklock(name)
- char *name;
- {
- register int ret;
- pid_t lpid = -1;
- char alpid[SIZEOFPID+2]; /* +2 for '\n' and NULL */
- int fd;
-
- #ifdef DEBUG
- fprintf(debugfp,"chklock(): At the top.\n");
- fflush(debugfp);
- #endif
-
- fd = open(name, O_RDONLY);
-
- if (fd == -1) {
- if (errno == ENOENT) /* file does not exist -- OK */
- #ifdef DEBUG
- fprintf(debugfp,"chklock(): file does not exist. Successful.\n");
- fflush(debugfp);
- #endif
- return(0);
- goto unlk;
- }
-
- ret = read(fd, (char *) alpid, SIZEOFPID+1); /* +1 for '\n' */
-
- (void) close(fd);
-
- if (ret != (SIZEOFPID+1)) {
- goto unlk;
- }
-
- lpid = (pid_t) strtol(alpid, (char **) NULL, 10);
-
- if (((ret=kill(lpid, 0)) == 0 || errno == EPERM)) {
- #ifdef DEBUG
- fprintf(debugfp,"chklock(): Lock File--process still active--not removed.\n");
- fflush(debugfp);
- #endif
- return(-1);
- }
- else {
- #ifdef DEBUG
- fprintf(debugfp,"chklock(): OK to remove lock file (%s).\n",name);
- fflush(debugfp);
- #endif
- }
-
- unlk:
-
- if (unlink(name) != 0) {
- #ifdef DEBUG
- fprintf(debugfp,"chklock(): unlink() failed in chklock().\n");
- fflush(debugfp);
- #endif
- return(-1);
- }
-
- #ifdef DEBUG
- fprintf(debugfp,"chklock(): Successful.\n");
- fflush(debugfp);
- #endif
- return(0);
- }
-
-
-
- static int onelock(pid,tempfile,name)
- char *pid;
- char *tempfile, *name;
- {
- register int fd;
-
- #ifdef DEBUG
- fprintf(debugfp,"onelock(): At the top.\n");
- fprintf(debugfp,"onelock(): pid = (%s)\n",pid);
- fprintf(debugfp,"onelock(): tempfile = (%s)\n",tempfile);
- fprintf(debugfp,"onelock(): name = (%s)\n",name);
- fflush(debugfp);
- #endif
- fd=creat(tempfile, (mode_t) 0444);
- if(fd < 0){
- #ifdef DEBUG
- fprintf(debugfp,"onelock(): creat(%s, 0444) failed\n");
- fflush(debugfp);
- #endif
- if((errno == EMFILE) || (errno == ENFILE))
- (void) unlink(tempfile);
- return(-1);
- }
-
- (void) write(fd, pid, SIZEOFPID+1); /* +1 for '\n' */
- (void) chmod(tempfile, (mode_t) 0444);
- (void) chown(tempfile, 0, 0);
- (void) close(fd);
-
- if(link(tempfile,name)<0){
- #ifdef DEBUG
- fprintf(debugfp,"onelock(): link(%s, %s) failed\n",tempfile, name);
- fflush(debugfp);
- #endif
- if(unlink(tempfile)< 0){
- #ifdef DEBUG
- fprintf(debugfp,"onelock(): unlink(%s) failed\n",tempfile);
- fflush(debugfp);
- #endif
- }
- return(-1);
- }
-
- if(unlink(tempfile)<0){
- #ifdef DEBUG
- fprintf(debugfp,"onelock(): unlink(%s) failed.\n",tempfile);
- fflush(debugfp);
- #endif
- }
-
- return(0);
- }
-
-
- int set_lock()
- {
- char pid[SIZEOFPID+2];
- char tempfile[128];
-
- sprintf(pid,"%d",getpid());
- sprintf(tempfile,"%s.%s",LOCKFILE,pid);
- if (onelock(pid,tempfile,LOCKFILE) == -1) {
- (void)unlink(tempfile);
- if (chklock(LOCKFILE))
- return(-1);
- else {
- if (onelock(pid,tempfile,LOCKFILE)) {
- (void)unlink(tempfile);
- return(-2);
- }
- }
- }
- return(0);
- }
-
-
-
- void tell_mac(message_code,local_message)
- int message_code;
- char *local_message;
- {
- AppleEvent theAppleEvent;
- AppleEvent reply;
- AESendMode sendMode;
- int sessionID=1;
- AESendPriority sendPriority;
- AEDesc target;
- long timeOutInTicks;
-
- #ifdef DEBUG
- fprintf(debugfp,"In tell_mac().\n");
- fprintf(debugfp," messagecode = (%d).\n",message_code);
- fprintf(debugfp," local_message = (%s).\n",local_message);
- fprintf(debugfp," strlen(local_message) = (%d).\n",strlen(local_message));
- fflush(debugfp);
- #endif
-
- (void)AECreateDesc(typeSessionID,&sessionID,sizeof(sessionID),&target);
-
- (void)AECreateAppleEvent(kAEAUXSuite,kAEDiscMsg,&target,
- kAutoGenerateReturnID,kAnyTransactionID,&theAppleEvent);
-
- (void)AEPutParamPtr(&theAppleEvent,keySessionID,typeInteger,&sessionid,sizeof(sessionid));
-
- (void)AEPutParamPtr(&theAppleEvent,keyMessageCode,typeInteger,
- &message_code,sizeof(message_code));
- (void)AEPutParamPtr(&theAppleEvent,keyMsgString,typeChar,
- local_message,strlen(local_message));
-
- sendMode = kAENoReply;
- sendPriority = kAENormalPriority;
- timeOutInTicks = kAEDefaultTimeout;
-
- AESend(&theAppleEvent,&reply,sendMode,sendPriority,timeOutInTicks,NULL,NULL);
-
- AEDisposeDesc(&target);
- AEDisposeDesc(&theAppleEvent);
-
- #ifdef DEBUG
- fprintf(debugfp,"Leaving tell_mac().\n");
- fflush(debugfp);
- #endif
-
- }
-
-
- int chkaccess(user,service)
- char *user;
- char *service;
- {
- FILE *fp;
- char line[512];
- char *cp;
- char *toke;
-
- if ((fp = fopen(ACLIST,"r")) == NULL) return(-1);
- #ifdef DEBUG
- fprintf(debugfp,"In chkaccess(): fopen() succeeded\n");
- fflush(debugfp);
- #endif
- while (fgets(line,sizeof(line)-1,fp)) {
- if ((strncmp(line,service,strlen(service)) == 0) &&
- (line[strlen(service)] == ':')) {
- cp = line+strlen(service)+1;
- toke = strtok(cp,",\n");
- while (toke != NULL) {
- if (strcmp(user,toke) == 0) {
- fclose(fp);
- #ifdef DEBUG
- fprintf(debugfp,"In chkaccess(): Access granted.\n");
- fflush(debugfp);
- #endif
- return(0);
- }
- toke = strtok(NULL,",\n");
- }
- fclose(fp);
- #ifdef DEBUG
- fprintf(debugfp,"In chkaccess(): Access denied.\n");
- fflush(debugfp);
- #endif
- return(-1);
- }
- }
- fclose(fp);
- #ifdef DEBUG
- fprintf(debugfp,"In chkaccess(): Access denied.\n");
- fflush(debugfp);
- #endif
- return(-1);
- }
-
-
-
- void setup_debug(program_name)
- char *program_name;
- {
- char debugfile[64];
-
- sprintf(debugfile,"/tmp/%s.debug",program_name);
- debugfp = fopen (debugfile, "w");
- /*
- * setvbuf(debugfp,NULL,NULL,_IONBF);
- */
- }
-
-
-
- void set_access(argc,argv)
- int argc;
- char *argv[];
- {
- int i, retval;
- char *user;
- struct passwd *pw;
-
- #ifdef DEBUG
- fprintf (debugfp, "Mandeld: Starting up Mandeld server...\n\n");
- fprintf (debugfp, "Initial gid values:\n");
- fprintf (debugfp, " Real gid = (%d)\n",getgidx(ID_REAL));
- fprintf (debugfp, " Effective gid = (%d)\n",getgidx(ID_EFFECTIVE));
- fprintf (debugfp, " Saved gid = (%d)\n",getgidx(ID_SAVED));
- fprintf (debugfp, "Initial uid values:\n");
- fprintf (debugfp, " Real uid = (%d)\n",getuidx(ID_REAL));
- fprintf (debugfp, " Effective uid = (%d)\n",getuidx(ID_EFFECTIVE));
- fprintf (debugfp, " Saved uid = (%d)\n",getuidx(ID_SAVED));
- fprintf (debugfp, " Login uid = (%d)\n\n",getuidx(ID_LOGIN));
- fflush(debugfp);
- #endif
-
- user = NULL;
- if (argc > 1) {
- i = 2;
- while (i <= argc) {
- if (argv[i-1][0] == '-') {
- if (strncmp(argv[i-1],"-d",2) == 0) {
- if (argv[i-1][2] == '\0') {
- if (i < argc) {
- i++;
- debuglevel=atoi(argv[i-1]);
- }
- }
- else
- debuglevel=atoi(argv[i-1]+2);
- }
- }
- else {
- user = argv[i-1];
- }
- i++;
- }
- }
-
- if (user != NULL) {
- /*
- * if user is not in ACL or if user is "guest" then set IDs to
- * deny power to the user. Even if "guest" is in ACL we will
- * deny him power.
- */
- if ((chkaccess(user,"mandel") < 0) || (strcmp(user,"guest") == 0)) {
- setpwent();
- pw = getpwnam(user);
- if (pw == NULL) {
- /* ERROR */
- shutdown_now();
- }
- else {
- endpwent();
- /* set real and effective IDs to pw->pw_uid */
- retval=0;
- retval+=setgidx(ID_REAL|ID_EFFECTIVE|ID_SAVED,pw->pw_gid);
- retval+=setuidx(ID_REAL|ID_EFFECTIVE|ID_SAVED|ID_LOGIN,pw->pw_uid);
- if (retval < 0) {
- /* ERROR */
- #ifdef DEBUG
- fprintf (debugfp, "Retval from setuidx() is less than zero...\n");
- fprintf (debugfp, "Calling shutdown_now()!\n");
- fflush(debugfp);
- #endif
- shutdown_now();
- }
- #ifdef DEBUG
- else {
- fprintf (debugfp, "New gid values:\n");
- fprintf (debugfp, " Real gid = (%d)\n",getgidx(ID_REAL));
- fprintf (debugfp, " Effective gid = (%d)\n",getgidx(ID_EFFECTIVE));
- fprintf (debugfp, " Saved gid = (%d)\n",getgidx(ID_SAVED));
- fprintf (debugfp, "New uid values:\n");
- fprintf (debugfp, " Real uid = (%d)\n",getuidx(ID_REAL));
- fprintf (debugfp, " Effective uid = (%d)\n",getuidx(ID_EFFECTIVE));
- fprintf (debugfp, " Saved uid = (%d)\n",getuidx(ID_SAVED));
- fprintf (debugfp, " Login uid = (%d)\n\n",getuidx(ID_LOGIN));
- fflush(debugfp);
- }
- #endif
- }
- }
- else {
- /* We have an empowered user */
- #ifdef DEBUG
- fprintf(debugfp,"In set_access(): This is an empowered user.\n");
- fflush(debugfp);
- #endif
- if ((retval=set_lock()) < 0) {
- switch (retval) {
- case -1: tell_mac(ALREADY_LOCKED,"");
- #ifdef DEBUG
- fprintf(debugfp,"Discus daemon already in use by empowered user.\n");
- fflush(debugfp);
- #endif
- break;
- case -2: tell_mac(CANT_LOCK,"");
- #ifdef DEBUG
- fprintf(debugfp,"set_lock() cannot create lock file.\n");
- fflush(debugfp);
- #endif
- break;
- default: tell_mac(UNKNOWN_ERROR,"");
- #ifdef DEBUG
- fprintf(debugfp,"Unknown error in set_lock().\n");
- fflush(debugfp);
- #endif
- break;
- }
- shutdown_now();
- }
- }
- }
-
-
- serverVersion = (char *)malloc(1+strlen(VERSION));
- strcpy(serverVersion,VERSION);
- databuf = (char *)malloc(DATABUFSIZE);
- }
-
-